import Vue from 'vue'
const defaultFormat = 'YYYY-MM-DD HH:mm:ss'

// 时间过滤
Vue.filter('dateFilter', (date, format = defaultFormat) => {
  if (typeof date === 'number') date = date * 1000
  const dayjs = Vue.prototype.$dayjs
  const now = dayjs()
  date = dayjs(date)
  if (format === 'relative' && now.diff(date, 'day') <= 2) {
    return date.fromNow()
  } else {
    format = format === 'relative' ? defaultFormat : format
    return date.format(format)
  }
})

// 价格过滤
Vue.filter('priceFilter', price => {
  return (Number(price) || 0).toFixed(2)
})
